package schoolsource.security;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;

import schoolsource.util.SchoolLogger;

/**
 *
 * @author Steven Steele
 * Copyright 2003
 *
 * A container for user information that has a timeout where, after 10 minutes of inactivity,
 * the security token removes itself from independent storage.
 */
public class SecurityToken extends Thread {
	// The key associated with the security token
	private String tokenKey = null;
	// The length of time before a timeout occurs
	private static long TIMEOUT = 1000 * 60 * 10; // 10 minutes
	// Indicates whether the timer should be reset
	private boolean continueLoop = true;
	// The container within the token for user information storage
	private HashMap hm = new HashMap();

	/**
	 * Sets the key of the security token.
	 * @param newTokenKey The new key for the security token.
	 */
	public void setTokenKey(String newTokenKey) {
		tokenKey = newTokenKey;
	}

	/**
	 * Starts the security token timer.
	 */
	public void run() {
		while (continueLoop) {
			try {
				//  SchoolLogger.logger.log(Level.INFO, "going to sleep");
				sleep(TIMEOUT);
				SecurityAuthorizer.removeToken(tokenKey);
				continueLoop = false;
			} catch (InterruptedException ie) {
				// do nothing
			}
		}
	}

	/**
	 * Stops the security token timer. The security token may then die or restart its timer.
	 */
	public void stopTimer() {
		continueLoop = false;
		this.interrupt();
	}

	/**
	 * Stores a value in the security token
	 * @param key The key used to store the value.
	 * @param o The value to be stored.
	 */
	public void putValue(String key, Object o) {
		hm.put(key, o);
	}

	/**
	 * Retrieves a value stored in the security token.
	 * @param key The key for the value to be retrieved.
	 * @return The value associated with the key. If no value is associated with the key,
	 * null is returned.
	 */
	public Object getValue(String key) {
		return hm.get(key);
	}

	/**
	 * Removes a value stored in the security token.
	 * @param key The key for the value to be removed.
	 * @return The value associated with the key. If no value is associated with the key,
	 * null is returned.
	 */
	public Object removeValue(String key) {
		return hm.remove(key);
	}

	/**
	 * Returns the string token associated with the security token.
	 * @return The string token associated with the security token.
	 */
	public String toString() {
		return tokenKey;
	}

	/**
	 * Prints all values stored in the security token in addition to the string
	 * token associated with the security token.
	 */
	public void printValues() {
		Set keys = hm.keySet();
		String key;
		Iterator i = keys.iterator();
		SchoolLogger.logger.log(Level.INFO, "Token: " + tokenKey);
		SchoolLogger.logger.log(Level.INFO, "List token values");
		while (i.hasNext()) {
			key = (String) i.next().toString();
			SchoolLogger.logger.log(
				Level.INFO,
				"    " + key + " : " + hm.get(key));
		}
	}
}